﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GamePlugin.Logics;
using GamePlugin.Logics.Data.Auction;
using System.Windows.Forms;
using GamePlugin.IO;

namespace GamePlugin.Controls
{
    /// <summary>
    /// Interaction logic for AuctionControl.xaml
    /// </summary>
    public partial class AuctionControl : System.Windows.Controls.UserControl
    {
        public AuctionControl()
        {
            InitializeComponent();

            this.Init();
        }

        private void Init()
        {
            this.AuctionItemListBox.DataContext = Workspace.Instance.AuctionItemList;

            if (Workspace.Instance.AuctionItemList.Children.Count > 0)
            {
                this.AuctionItemListBox.SelectedIndex = 0;
            }
        }

        private void AuctionItemListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                this.DataContext = e.AddedItems[0] as AuctionItem;
                (e.AddedItems[0] as AuctionItem).UpdateDisplayName();


            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog1.InitialDirectory = IOManager.Instance.DataFolderPath;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.FileName = "DefaultAuction.pak";
            saveFileDialog1.Filter = "Property File|*.pak";
            saveFileDialog1.Title = "Save property data";
            ;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog1.FileName == null || saveFileDialog1.FileName.Length == 0)
                    return;
                //IOManager.Instance.SaveSearchList(searchListBox.DataContext as SearchInfoItemList, saveFileDialog1.FileName);
                IOManager.Instance.SaveAuctionData(Workspace.Instance.AuctionItemList, saveFileDialog1.FileName);
            }
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.InitialDirectory = IOManager.Instance.DataFolderPath;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FileName = "DefaultAuction.pak";
            openFileDialog.Filter = "Property File|*.pak";
            openFileDialog.Title = "Load property data";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog.FileName == null || openFileDialog.FileName.Length == 0)
                    return;
                IOManager.Instance.LoadAuctionData(Workspace.Instance.AuctionItemList, openFileDialog.FileName);
            }
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            AuctionItem item = sender as AuctionItem;
            if (item == null)
                return;

            Workspace.Instance.AuctionItem = item;
            item.UpdateDisplayName();
        }
    }
}
